<template>
  <a-modal
    title="导入监播汇总"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          label="所属发布"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-select style="width: 80%" placeholder="请选择所属发布" v-decorator="['managerId',{rules: [{ required: true, message: '请选择所属发布！' }]}]" >
            <a-select-option v-for="(item,index) in pushManage" :key="index" :value="item.id" @click="selectedValue(item.id)">{{ item.lineName }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item
          label="文件信息"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-upload
            :customRequest="handleImport"
            :multiple="true"
            :showUploadList="false"
            name="file"
          >
            <a-button>
              <a-icon type="upload"/>
              导入
            </a-button>
          </a-upload>
          <div v-if="uploadedFileName">上传的文件：{{uploadedFileName}}</div>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import { monitoringGatherImport } from '@/api/modular/system/monitoringGatherManage'
  import { pushManageList } from '@/api/modular/system/pushManageManage'
  export default {
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this),
        pushManage: [],
        gatherData: '',
        byManagerId: '',
        formData: new FormData(),
        uploadedFileName: ''
      }
    },
    methods: {
      // 初始化方法
      import (record) {
        this.uploadedFileName = ''
        this.formData = new FormData()
        this.visible = true
        this.getManageList()
      },
      getManageList() {
        pushManageList().then((res) => {
          this.pushManage = res.data
        })
      },
      selectedValue(value) {
        this.byManagerId = value
        console.log('selected : ', value)
      },
      /**
       * 提交表单
       */
      handleImport(data) {
        this.formData.append('file', data.file)
        this.uploadedFileName = data.file.name
        // 模拟上传成功的回调
        data.onSuccess()
      },
      handleSubmit () {
        this.formData.append('managerId', this.byManagerId)
        monitoringGatherImport(this.formData).then((res) => {
          if (res.success) {
            this.$message.success('上传成功')
            this.handleCancel()
          } else {
            this.$message.error('上传失败：' + res.message)
          }
        })
      },
      handleCancel () {
        this.form.resetFields()
        this.visible = false
      }
    }
  }
</script>
